package com.zhong.gang.applib.utils;

import android.app.Activity;

import java.util.ArrayList;
import java.util.List;

public class ActivityController {
    private static ActivityController controller;
    private static final List<Activity> activities = new ArrayList<>();
    public static ActivityController getInstance(){
        if (controller == null)
            controller = new ActivityController();
        return controller;
    }

    public void addAct(Activity activity) {
        if (activity == null)
            return;
        for (Activity act : activities) {
            if (activity.getClass() == act.getClass()) {
                return;
            }
        }
        activities.add(activity);
    }

    public void removeAct(Activity activity){
        if (activity == null)
            return;
        activities.remove(activity);
    }
    public void closeAct(Class mClass){
        if (mClass == null)
            return;
        for (Activity activity:activities){
            if (activity != null &&activity.getClass() == mClass){
                if (!activity.isFinishing())
                activity.finish();
                activities.remove(activity);
                return;
            }
        }
    }
    public void closeActNot(Class mClass){
        for (Activity activity:activities){
            if (activity != null &&activity.getClass() != mClass){
                if (!activity.isFinishing())
                activity.finish();
                activities.remove(activity);
            }
        }
    }
}
